<?php

namespace App\Http\Controllers\Guard\Goods;

use App\Http\Controllers\Common\ApiController;
use App\Models\Goods\Goods;
use Illuminate\Http\Request;

class GoodsController extends ApiController{


    public function __construct() {
        $this->instance = new Goods();
    }

    public function lst(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',50);
        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);
        return responseSuccess($records->items(),$records->total());

    }

    public function add(Request $request) {
        $info = $request->input();

        $this->validate($request, [

        ],[],[

        ]);
        $thumbsPath = array_column($info['thumbs'], 'path');
        //如果是编辑操作且没有图片传过来，则说明只是编辑了其他信息
        if($info['id']>0 && count($thumbsPath)<=0){
            unset($info['thumbs']);
        }else{
            $info['thumbs'] =  implode(',', $thumbsPath);
        }
        $record = $this->instance->updateOrCreate(['id'=>$info['id']??0],$info);
        return responseSuccess($record->toArray(),1);
    }
}
